//
//  ClassifyViewController.m
//  Mahjong
//
//  Created by mc on 16/8/29.
//  Copyright © 2016年 Elliot. All rights reserved.
//

#import "ClassifyViewController.h"
#import "NearlySelectCell.h"
#import "ClassifyCollectionViewCell.h"
#import "CXSearchController.h"
#import "GoodsDetailViewController.h"
#import "GoodsListViewController.h"
#import "Header.h"
#import "ClassModel.h"
#define SCREEWEITH [UIScreen mainScreen].bounds.size.width
#define ScreeHEIGHT [UIScreen mainScreen].bounds.size.height

@interface ClassifyViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UITextFieldDelegate>

{
    UITextField *seachField;
    UIView *leftbackView;
    
}


@property(nonatomic, strong)NSMutableArray *nearlySelectArr;
@property(nonatomic, strong)NSMutableArray *categorySelectArr;
@property(nonatomic, strong)NSMutableArray *distanceSelectArr;

@property (nonatomic, strong)NSMutableArray *nearArray;
@property (nonatomic, strong)NSMutableArray *cateArray;
@property (nonatomic, strong)NSMutableArray *distArray;
@property(nonatomic, strong)UITableView *NearlySelectTableview;
@property (nonatomic, strong) UICollectionView *myCollectionVIew;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *dateArray;
@property (nonatomic, strong) NSMutableDictionary *dateDic;
@property (nonatomic, assign) NSInteger num;
@end

@implementation ClassifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBar.translucent = NO;
//    self.nearlySelectArr = [NSMutableArray arrayWithObjects:@"内科", @"外科", @"儿科", @"妇科", @"皮肤科", @"五官科", @"传染科", @"心理科", @"中医科", @"肿瘤科", @"男科", @"其他",nil];
//    self.categorySelectArr = [NSMutableArray arrayWithObjects:@"皮肤类", @"五官类", @"小孩类", @"大人类",  @"其他类",nil];
//    self.distanceSelectArr = [NSMutableArray arrayWithObjects:@"100米", @"200米", @"300米", @"400米", @"其他",nil];
//    self.cateArray = [NSMutableArray arrayWithObjects:@"NO", @"NO", @"NO", @"NO", @"NO",nil];
//    self.distArray = [NSMutableArray arrayWithObjects:@"NO", @"NO", @"NO", @"NO", @"NO",nil];
//    self.nearArray = [NSMutableArray arrayWithObjects:@"NO", @"NO", @"NO", @"NO", @"NO", @"NO", @"NO", @"NO", @"NO", @"NO", @"NO", @"NO",nil];
//    [self addtopnavigationUI];
    self.dataArray = [NSMutableArray array];
    self.dateArray = [NSMutableArray array];
    //顶部搜索,左边选择页面
    self.dateDic = [NSMutableDictionary dictionary];
    [self addOtherUI];
    [self addNearlySelectTableviewUI];
}

- (void)addtopnavigationUI {
    UIView *navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375, CGRectGetMaxY(self.navigationController.navigationBar.frame))];
    navView.backgroundColor = [UIColor colorWithRed:0.212 green:0.722 blue:0.412 alpha:1.000];
    [self.view addSubview:navView];
    
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(10, CGRectGetMinY(self.navigationController.navigationBar.frame)+5, 20, CGRectGetHeight(self.navigationController.navigationBar.frame)-10);
    [backBtn setImage:[UIImage imageNamed:@"shequ_fanghui"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backVC) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:backBtn];
    
    
}

- (void)backVC{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
//    [self.navigationController popViewControllerAnimated:YES];
}

- (void)addOtherUI {
    UIView *backVIew = [[UIView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 4, 30, [UIScreen mainScreen].bounds.size.width / 2 + 20, 30)];
    self.navigationItem.titleView = backVIew;
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width / 2 + 20, 30)];
    lable.text = @"             麻将机";
    lable.font = [UIFont systemFontOfSize:15];
    lable.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1];
    lable.textColor = [UIColor grayColor];
    lable.layer.borderColor = [UIColor colorWithWhite:0.978 alpha:0.55].CGColor;
    lable.layer.masksToBounds = YES;
    [backVIew addSubview:lable];
    //    lable.layer.borderWidth = 2;
    lable.layer.cornerRadius = 5;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 20, 20)];
    //    , 30, 20, 20]
    //    imageView.backgroundColor = [UIColor grayColor];
    [imageView setImage:[UIImage imageNamed:@"nav_icon_sousuo"]];
    [lable addSubview:imageView];
    UIButton *seachButton = [UIButton buttonWithType:UIButtonTypeSystem];
    seachButton.frame = lable.frame;
    [seachButton addTarget:self action:@selector(seachAction) forControlEvents:UIControlEventTouchUpInside];
    [backVIew addSubview:seachButton];
    seachButton.backgroundColor = [UIColor clearColor];
    
    
    
//    seachField = [[UITextField alloc] initWithFrame:CGRectMake(10, 20, SCREEWEITH-20, 30)];
//    seachField.placeholder = @"麻将机";
//    seachField.backgroundColor = [UIColor colorWithWhite:0.969 alpha:1.000];
//    seachField.delegate = self;
//    seachField.font = [UIFont systemFontOfSize:14];
    
    //    textfield.clearButtonMode = UITextFieldViewModeWhileEditing;
    //    textfield.returnKeyType = UIReturnKeyDone;
    //    textfield.delegate = self;
//    seachField.layer.cornerRadius = CGRectGetHeight(seachField.frame)/2;
//    seachField.layer.masksToBounds = YES;
//    [self.navigationController.view addSubview:seachField];
//    [self.view addSubview:seachField];
//    [TextFiledAddImage setLeftViewWithTextField:seachField imageName:@"shequ_sousuo_gray"];
    
    leftbackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, (480*SCREEWEITH)/1242, ScreeHEIGHT)];
    leftbackView.backgroundColor = [UIColor colorWithWhite:0.976 alpha:1.000];
    [self.view addSubview:leftbackView];
    

    
}

-(void)seachAction{
    CXSearchController *search = [[CXSearchController alloc] init];
    search.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:search animated:YES];
    
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    NSLog(@"123");
    return YES;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [seachField resignFirstResponder];
}
- (void)buttonSelect:(UIButton *)sender {
    //    sender.selected = !sender.selected;
    
    if (sender.tag == 900) {
        self.num = 0;
        [self.myCollectionVIew reloadData];
       
    }
    if (sender.tag == 901) {
        
        self.num = 1;
        [self.myCollectionVIew reloadData];
    }
    if (sender.tag == 902) {
        self.num = 2;
        [self.myCollectionVIew reloadData];
        
        
    }
    if (sender.tag == 903) {
        self.num = 3;
        [self.myCollectionVIew reloadData];
        
        
    }
    if (sender.tag == 904) {
        self.num = 4;
        [self.myCollectionVIew reloadData];
        
        
    }
    if (sender.tag == 905) {
        self.num = 5;
        [self.myCollectionVIew reloadData];
        
        
    }
    for (UIButton *button in leftbackView.subviews) {
        if (sender.tag == button.tag) {
            button.selected = YES;
            button.backgroundColor = [UIColor whiteColor];
        }else {
            button.selected = NO;
            button.backgroundColor = [UIColor colorWithWhite:0.976 alpha:1.000];
        }
    }
    
    
    
    
}

- (void)addNearlySelectTableviewUI {
    
    
//    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//    layout.itemSize = CGSizeMake(50, 50);
//    layout.sectionInset = UIEdgeInsetsMake(1, 2, 1, 2);
//    layout.minimumLineSpacing = 1;
//    layout.minimumInteritemSpacing = 1;
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    //设置item大小
    flowLayout.itemSize = CGSizeMake((SCREEWEITH-(480*SCREEWEITH)/1242)/2-10, (SCREEWEITH-(480*SCREEWEITH)/1242)/2-10);
    //设置item与屏幕四周的间距
    //边界距离:上,左,下,右
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    //设置最小行间距
    flowLayout.minimumLineSpacing = 1;
    //设置最小列间距
    flowLayout.minimumInteritemSpacing = 1;

    

    
    self.myCollectionVIew = [[UICollectionView alloc] initWithFrame:CGRectMake((480*SCREEWEITH)/1242, 0, SCREEWEITH-(480*SCREEWEITH)/1242, ScreeHEIGHT) collectionViewLayout:flowLayout];
    [self.view addSubview:self.myCollectionVIew];
    self.myCollectionVIew.delegate = self;
    self.myCollectionVIew.dataSource = self;
    self.myCollectionVIew.backgroundColor = [UIColor clearColor];
    [self.myCollectionVIew registerNib:[UINib nibWithNibName:@"ClassifyCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    self.num = 0;
    [self createData];
    
   
}
-(void)createData{
    [AppTools postURL:@"http://api03.m0571.com/WebJson.aspx?book=GetType" data:nil success:^(id result) {
        NSArray *array = [result[0] objectForKey:@"date"];
        for (NSMutableDictionary *dic in array) {
            ClassModel *model = [[ClassModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
//            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
//            [dic setObject:model.typedetail forKey:model.typename];
            [self.dataArray addObject:model];
            [self.dateArray addObject:model.typename];
        }
        [self.myCollectionVIew reloadData];
        
        
        for (NSInteger i=0; i<self.dateArray.count; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(0, ((200*ScreeHEIGHT)/2208)*i, (480*SCREEWEITH)/1242, (200*ScreeHEIGHT)/2208);
            [button setTitle:self.dateArray[i] forState:UIControlStateNormal];
//            [button setImage:[UIImage imageNamed:imageArr[i]] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [leftbackView addSubview:button];
//            [button setImage:[UIImage imageNamed:imageSelectArr[i]] forState:UIControlStateSelected];
            [button setTitleColor:[UIColor colorWithRed:49/255. green:194/255. blue:124/255. alpha:1.000] forState:UIControlStateSelected];
            [button setTitleColor:[UIColor colorWithRed:56/255. green:56/255. blue:56/255. alpha:1.000] forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
            button.tag = 900+i;
            if (i==0) {
                button.selected = YES;
                button.backgroundColor = [UIColor whiteColor];
            }
            [button addTarget:self action:@selector(buttonSelect:) forControlEvents:UIControlEventTouchUpInside];
        }

        
    } error:^(NSError *error) {
        
        
    }];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (self.dataArray.count == 0) {
        return 0;
    }
      ClassModel *mo = self.dataArray[self.num];

    if (mo.typedetail == nil) {
        return 0;
    }
    return mo.typedetail.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ClassifyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    ClassModel *mo = self.dataArray[self.num];

    cell.titleLabel.text = [mo.typedetail[indexPath.row] objectForKey:@"detailname"];

    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    GoodsListViewController *goods= [[GoodsListViewController alloc] init];
    goods.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:goods animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
